package gui.cen.tree;

import core.tree.Tree;
import core.utils.Utils;
import gui.cen.Center;
import gui.utils.Para;
import gui.utils.Slider;
import gui.utils.SliderWrap;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class TreeOpts extends JPanel implements ActionListener{
    private JButton nodes, labels,savePDF;
    private Center center;
    public static int HEIGHT_OF_OPTIONS = 180;
    public TreeOpts(Center center){
            this.setLayout(new BorderLayout());
            this.center = center;
            this.setPreferredSize(new Dimension(Center.WIDGE_WIDTH, HEIGHT_OF_OPTIONS));
            this.setBackground(new Color(102, 102, 102));
            this.setBorder(BorderFactory.createLineBorder(new Color(178, 178, 178), 2));

            JPanel p = new JPanel();
            p.setOpaque(false);
            p.setBorder(BorderFactory.createEmptyBorder(0,5,0,5));
            p.setLayout(new GridLayout(5,1));
            p.add(new SliderWrap(Slider.TMAG, 10000, 100, Tree.INITIAL_TREE_MAG, "Zoom ", 110, 3, center, false, 0, new Color(178, 178, 178), new DecimalFormat("#0.00")));
            p.add(new SliderWrap(Slider.ROTATE, 360, 0, 0, "Rotate ", 110, 3, center, false, 0, new Color(178, 178, 178), new DecimalFormat("#0.00")));
            p.add(Utils.getButton(labels = new JButton("Labels"), 180, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Display Labels"));
            Para.DISPLAY_LEAF_TITLES = false;
            p.add(Utils.getButton(nodes = new JButton("Nodes"), 180, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Display nodes"));
            Para.DISPLAY_LEAF_NODES = true;
            nodes.setForeground(Color.RED);
            p.add(Utils.getButton(savePDF = new JButton("Save"), 180, 25, new Color(102, 102, 102), new Color(178, 178, 178), this, "Save tree as PDF"));
            this.add(p, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent evt){
        Object source = evt.getSource();
        if(Para.TREE!=null){
            if(source == savePDF){
                try{center.getTreeDisplay().getTreePane().makePDF(Utils.getFile(new JPanel(), false).getSelectedFile().getPath());}catch(Exception e){}
            }
            else if(source == nodes){
                if(Para.DISPLAY_LEAF_NODES==true){
                    Para.DISPLAY_LEAF_NODES = false;
                    nodes.setForeground(new Color(102, 102, 102));
                }
                else if(Para.DISPLAY_LEAF_NODES==false){
                    Para.DISPLAY_LEAF_NODES = true;
                    nodes.setForeground(Color.RED);
                }
                center.updateTree();
            }
            else if(source == labels){
                if(Para.DISPLAY_LEAF_TITLES==true){
                    Para.DISPLAY_LEAF_TITLES = false;
                    labels.setForeground(new Color(102, 102, 102));
                }
                else if(Para.DISPLAY_LEAF_TITLES==false){
                    Para.DISPLAY_LEAF_TITLES = true;
                    labels.setForeground(Color.RED);
                }
                center.updateTree();
            }
        }
        else{center.msg("No Tree Available");}
    }
}
